﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ArchiveLink;

namespace FoldersForDVD
{
    class Program
    {
        protected const char REPLACE_STRING = ' ';
        protected static readonly HashSet<char> InvalidChars = new HashSet<char>(Path.GetInvalidFileNameChars());

        public static string GetFilesystemSaveString(string value)
        {
            var result = new StringBuilder(value.Length);
            foreach (char t in value)
            {
                if (!InvalidChars.Contains(t))
                {
                    result.Append(t);
                }
                else
                {
                    //result.Append(REPLACE_STRING);
                }
            }
            return result.ToString();
        }

        static void Main(string[] args)
        {
            //var csvL = File.ReadAllLines(@"C:\Users\Steve\Desktop\dvds.csv",Encoding.Default);
            //var outp = new List<string>(csvL.Length);
            //foreach (var l in csvL)
            //{
            //    if (l.Contains("\\Disks\\") || l.Contains("\\SchmalzDVD\\"))
            //    {
                    
            //        outp.Add(l);
            //    }
            //}
            //File.WriteAllLines(@"C:\Users\Steve\Desktop\dvds2.csv",outp);
            //return;

            var diskPath = @"C:\Archiv\Disks";
            var schmalzPath = @"C:\Archiv\SchmalzDisks";
            var dvsRaw = File.ReadAllLines(@"C:\Users\Steve\Desktop\dvds2.csv");
            foreach (var dvdraw in dvsRaw)
            {
                var dvdSegs = dvdraw.Split(';');
                var path = dvdSegs[5];
                var pathSegs = path.Split('\\');

                var fName = Path.GetFileNameWithoutExtension(path);
                var category = pathSegs[2];
                var diskId = pathSegs[3];
                var type = "dvd";
                if (category == "SchmalzDVD")
                {
                    diskId = "einer unbekannten Schmalz-DVD";
                }
                else if (diskId=="BD")
                {
                    diskId = "einer BluRay";
                    type = "bluray";
                }          
                else
                {
                    diskId = "DVD " + diskId;
                }

                var targetPath = category == "Disks" ? diskPath : schmalzPath;

                var xe = new XElement("discstub");
                var xeM = new XElement("message");
                xe.Add(xeM);
                xeM.SetValue("Der gewünschte Film befindet sich auf " + diskId);

                var linkFile = new FileInfo(Path.Combine(targetPath, GetFilesystemSaveString(fName) + "." + type + ".disc"));
                if (!linkFile.Directory.Exists)
                {
                    linkFile.Directory.Create();
                }

                xe.Save(linkFile.FullName);
            }
        }
    }
}
